package com.bwie.controller;

import com.bwie.espojo.ESItem;
import com.bwie.service.ItemService;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @description:
 * @author: ykk
 * @create: 2024-10-21 09:46
 **/
@RestController
@RequestMapping("/esitem")
public class ESController {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    ItemService itemService;

    /**
     * 创建es的索引库  用以存储数据
     * @return
     */
    @PostMapping("/addEs")
    public R addEs(){
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(ESItem.class);
        indexOps.create();
        Document mapping = indexOps.createMapping();
        boolean b = indexOps.putMapping(mapping);
        if(b){
            return R.OK("创建成功");
        }else{
            return R.ERROR(-1,"创建失败");
        }
    }

    /**
     * 创建es 分页查询
     * @param pageInfoVo
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody PageInfoVo pageInfoVo){
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        Integer pageNum=1;
        Integer pageSize=3;
        if(Objects.nonNull(pageInfoVo.getPageNum())){
            pageNum=pageInfoVo.getPageNum()-1;
        }
        if(Objects.nonNull(pageInfoVo.getPageSize())){
            pageSize=pageInfoVo.getPageSize();
        }
        PageRequest of = PageRequest.of(pageNum, pageSize);
        nativeSearchQueryBuilder.withPageable(of);
        ArrayList<ESItem> arrayList = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        SearchHits<ESItem> search = elasticsearchRestTemplate.search(nativeSearchQueryBuilder.build(), ESItem.class);
        long totalHits = search.getTotalHits();
        if(totalHits>0){
            List<SearchHit<ESItem>> searchHits = search.getSearchHits();
            searchHits.forEach(item->{
                ESItem content = item.getContent();
                arrayList.add(content);
            });
            hashMap.put("arrayList",arrayList);
            hashMap.put("total",totalHits);
        }
        return R.OK(hashMap);
    }
}
